let startCount = 0;
let tourDates;
let hot;

var mData = [
    ['出团日期', '产品编号', '产品名称', '计划位', '确认占位'],
];
//载入js表格库的css文件
window.main_API.injectStyle().then(res => {
    const injectStyle = document.createElement("link");
    injectStyle.type = "text/css";
    injectStyle.rel = "stylesheet";
    injectStyle.href = res;
    document.body.appendChild(injectStyle);
    injectStyle.onload = () => {
        //载入js表格库
        window.main_API.injectScript().then(res => {
            const injectScript = document.createElement("script");
            injectScript.src = res;
            document.body.appendChild(injectScript);
            injectScript.onload = () => {
                //载入js表格库的语言文件
                window.main_API.injectLanguageFile().then(res => {
                    const injectLanguage = document.createElement("script");
                    injectLanguage.type = "text/javascript";
                    injectLanguage.src = res;
                    document.body.appendChild(injectLanguage);
                    injectLanguage.onload = () => {
                        // init();
                        start();
                    }
                })
            }
        })
    }
})


// async function init() {
//     tourDates = await window.main_API.getTourDates();
//     start();
// }

async function updateTourDates() {
    tourDates = await window.main_API.getTourDates();

    mData = [
        ['出团日期', '产品编号', '产品名称', '计划位', '确认占位'],
    ];

    console.log(tourDates);
    for (const tourDate of tourDates) {
        mData.push([
            tourDate.date,
            tourDate.id,
            tourDate.name,
            tourDate.planNum,
            tourDate.actualNum
        ]);
    }
    console.log(mData);
    hot.updateData(mData);
}


function start() {


    hot = new Handsontable(document.getElementById("mainContainer"), {
        // configuration options, in the object literal notation
        licenseKey: "non-commercial-and-evaluation",
        data: mData,
        width: '100vw',
        height: '100vh',
        colHeaders: true,//显示列头
        rowHeaders: true,//显示行头
        customBorders: true,//显示边框
        dropdownMenu: true,//显示下拉菜单
        multiColumnSorting: true,//多列排序
        filters: true,//筛选器
        manualRowMove: true,//手动移动行
        language: 'zh-CN',//设置语言
        mergeCells: true,//合并单元格
        manualColumnFreeze: true,//手动冻结列
        manualColumnResize: true,//手动调整列宽
        className: ['htCenter', 'htMiddle'],//设置文本居中
        contextMenu: [
            'col_left',
            'col_right',
            'row_above',
            'row_below',
            '---------',
            // 'remove_col',
            'remove_row',
            '---------',
            // 'freeze_column',
            // 'unfreeze_column',
            '---------',
            'mergeCells'
        ],
    });

    // 监听afterSelectionEnd事件  
    hot.addHook('afterSelectionEnd', function (row, col, row2, col2, prevent) {
        if (row === 1) {
            console.log('当前选中' + 1);
            main_API.changeCurrentTourDataID(1)
        }
        if (row === 2) {
            console.log('当前选中' + 2);
            main_API.changeCurrentTourDataID(2)
        }
    });

    updateTourDates();
}